/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;

namespace DaveSexton.DocProject
{
	internal sealed class DocProjectPropertiesMenuCommand : MenuCommand
	{
		#region Public Properties
		/// <remarks>
		/// A static readonly field cannot be used here because an instance of <see cref="DocProjectPropertiesMenuCommand"/> can only
		/// be constructed when DocProject is being hosted by Visual Studio, but during testing .NET seems to be pre-loading the class
		/// constructor even before the field is used.
		/// </remarks>
		public static DocProjectPropertiesMenuCommand Instance
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
			get
			{
				if (instance == null)
					instance = new DocProjectPropertiesMenuCommand();

				return instance;
			}
		}

		public override string Tooltip
		{
			get
			{
				return Resources.Text.DocProjectPropertiesTooltip;
			}
		}

		public override EnvDTE.vsCommandStatus Status
		{
			get
			{
				object[] selectedItems = (object[]) VSEnvironmentHost.DTE.ToolWindows.SolutionExplorer.SelectedItems;

				if (selectedItems.Length == 1)
				{
					IDocProject project = DocProjectEnvironment.SelectedProject;

					if (project != null)
						return vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
				}

				return vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusInvisible;
			}
		}
		#endregion

		#region Private / Protected
		private static DocProjectPropertiesMenuCommand instance;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectPropertiesMenuCommand" /> class for internal use only.
		/// </summary>
		private DocProjectPropertiesMenuCommand()
			// NOTE: The singleton is not instantiated unless DocProject is being hosted by Visual Studio; i.e., the Add-In must be loaded and DTE will be set.
			: base(VSEnvironmentHost.FindCommandBar(IsProjectContextMenu), "DocProjectPropertiesMenuCommand", Resources.Images.DocProjectProperties, System.Drawing.Color.Fuchsia)
		{
		}
		#endregion

		#region Methods
		private static bool IsProjectContextMenu(Microsoft.VisualStudio.CommandBars.CommandBar bar)
		{
			return string.Equals(bar.Name, "Project", StringComparison.Ordinal)
				&& bar.Controls.Count > 20;
		}

		protected override void OnExecuted(EventArgs e)
		{
			IDocProject project = DocProjectEnvironment.SelectedProject; 

			if (project != null)
			{
				DocProjectPropertiesToolWindow window = (DocProjectPropertiesToolWindow)
					DocProjectEnvironment.ToolWindows[DocProjectPropertiesToolWindow.ToolWindowGuid];

				window.Project = project;
				window.Visible = true;

				base.OnExecuted(e);
			}
		}

		public override string GetStatusText(object status)
		{
			return Resources.Text.DocProjectPropertiesMenuItemCaption;
		}
		#endregion
	}
}
